package it.mozzicato.apkwizard;

import it.mozzicato.apkwizard.dto.*;
import it.mozzicato.apkwizard.model.*;

import java.awt.*;
import java.awt.font.*;
import java.util.*;

import javax.swing.*;
import javax.swing.table.*;

// Creato il 20/mag/2011

/**
 * @author Roberto Mozzicato
 *
 */
public class StatusRenderer implements ListCellRenderer, TableCellRenderer {
	private static final Color LIGHT_BACKGROUND = new Color(230, 230, 230);
	private static ListCellRenderer defaultListRenderer;
	private static TableCellRenderer defaultTableRenderer;

	static {
		JComboBox tmpCombo = new JComboBox();
		defaultListRenderer = tmpCombo.getRenderer();
		JTable tmpTable = new JTable(1,1);
		defaultTableRenderer = tmpTable.getCellRenderer(0, 0);
	}
	
	@Override
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		Component ret = defaultListRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
		
		Object item = list.getModel().getElementAt(index);
		return render(ret, item, index, isSelected);
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		Component ret = defaultTableRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

		Object item = ((AbstractResourceTableModel<?>) table.getModel()).getItemAt(row);
		return render(ret, item, row, isSelected);
	}

	private Component render(Component component, Object item, int index, boolean isSelected) {
		
		if(item instanceof StatusObject) {
			StatusObject statusObject = (StatusObject) item;
			Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>();	
			ResourceStatus status = statusObject.getStatus();
			switch (status) {
				case INSERTED:
					attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
					break;
				case UPDATED:
					attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
					attributes.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
					break;
				case DELETED:
					attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
					attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
					break;
			}
			
			if(attributes.size() > 0)
				component.setFont(component.getFont().deriveFont(attributes));
		}
			
		if(!isSelected) {
			if(index % 2 <= 0)
				component.setBackground(Color.white);
			else
				component.setBackground(LIGHT_BACKGROUND);
		}
		
		return component;
	}
}

